//
//  LSZEmoticonTools.swift
//  醒浪微博
//
//  Created by 雷顺哲 on 16/7/19.
//  Copyright © 2016年 LSZ. All rights reserved.
//

import UIKit

//一页显示20个数据
let numbersOfPage = 20
//读取表情工具类
class LSZEmoticonTools: NSObject {
    
//    定义一个单例的全局访问点
    static let shareTools = LSZEmoticonTools()
    
//    构造函数私有化
    private override init() {
        super.init()
    }
    
    
//    准备表情视图需要的数据
    lazy var allEmoticonArray: [[[LSZEmoticon]]] = {
        return [            
                self.pagesWithEmotions(self.defaultEmoticonArray),
                self.pagesWithEmotions(self.emojiEmoticonArray),
                self.pagesWithEmotions(self.lxhEmoticonArray)
                ]
    }()
    
//    创建bundle对象
    private lazy var emoticonBundle: NSBundle = {
        let path = NSBundle.mainBundle().pathForResource("Emoticons", ofType: "bundle")!

//        创建bundle对象
        let bundle = NSBundle(path: path)!
        
        return bundle
    }()
    
//    读取默认表情数据
    private lazy var defaultEmoticonArray: [LSZEmoticon] = {
        return self.loadEmoticonsWithPath("default/info.plist")
    }()
//    读取emoji表情数据
    private lazy var emojiEmoticonArray: [LSZEmoticon] = {
        return self.loadEmoticonsWithPath("emoji/info.plist")
    }()
//    读取浪小花表情数据
    private lazy var lxhEmoticonArray: [LSZEmoticon] = {
        return self.loadEmoticonsWithPath("lxh/info.plist")
    }()
    
    
    
//    读取表情的通用方法
    private func loadEmoticonsWithPath(subPath: String) -> [LSZEmoticon] {
//        获取bundle对象
        let bundle: NSBundle = self.emoticonBundle
//        获取info.plist文件路径
        let path = bundle.pathForResource(subPath, ofType: nil)!
//        获取plist文件内容
        let emoticonDicArray = NSArray(contentsOfFile: path)!
        
        var tempArr = [LSZEmoticon]()
        
        for dic in emoticonDicArray{
            let emotic = LSZEmoticon(dic: dic as! [String : AnyObject])
            
//            如果是图片需要处理路径
            if emotic.type == "0"{
//                取消path路径后缀info.plist
                let lastPath = (path as NSString).stringByDeletingLastPathComponent
                emotic.path = lastPath + "/" + emotic.png!
            }
            tempArr.append(emotic)
        }
        return tempArr
    }
    
    
//    把表情数据拆分成二维数组
    private func pagesWithEmotions(emoticon: [LSZEmoticon]) -> [[LSZEmoticon]] {
        
//        根据图片个数计算页数
        let page = (emoticon.count - 1) / numbersOfPage + 1
        
        var tempArray = [[LSZEmoticon]]()
        for i in 0 ..< page {
            
//            截取的索引
            let loc = i * numbersOfPage
//            截取的个数
            var len = numbersOfPage
            
//            如果截取的长度不够则取到剩余个数
            if loc + len > emoticon.count {
                len = emoticon.count - loc
            }
            
            let subArray = (emoticon as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as! [LSZEmoticon]
            tempArray.append(subArray)
        }
        return tempArray
    }
    

}
